package com.appspot.gaeforum309.db;

import java.util.Date;
import java.util.List;

import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.appspot.gaeforum309.core.ForumAPI;
import com.appspot.gaeforum309.pathing.Action;
import com.appspot.gaeforum309.pathing.PathableClass;
import com.appspot.gaeforum309.pathing.Action.ActionType;
import com.appspot.gaeforum309.pathing.PathBuilder.IPathable;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class DBConversation implements IPathable {
	public final static String tagName = "Conversation";
	
	public static final PathableClass pathable = new PathableClass(tagName, DBTopic.tagName){

		@Override
		public IPathable getObject(String keyStr) {
			return DBConversation.getConversation(keyStr);
		}
		
		@Override
		public void fillAction(Action.ActionType actionType, Action action) {
			switch(actionType)
			{
			case createChild:
				action.addField("Title", Action.FieldType.text, "");
				action.addField("Description", Action.FieldType.text, "");
				action.addField("Comment", Action.FieldType.longtext, "");
				break;
			}
		}
	};
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	@Persistent
	private String title;
	
	@Persistent
	private String description;

	@Persistent
	private Key topic;

	@Persistent
	private Date dateLastComment;

	public DBConversation(String title, String description, DBTopic topic) {
		super();
		this.title = title;
		this.description = description;
		this.topic = topic.getKey();
		this.dateLastComment = new Date();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Key getKey() {
		return key;
	}

	public Key getTopic() {
		return topic;
	}

	public Date getDateLastComment() {
		return dateLastComment;
	}
	
	public void setDateLastComment(Date dataNew) {
		this.dateLastComment = dataNew;
	}

	public String getKeyStr()
	{
		return KeyFactory.keyToString(getKey());
	}
	
	@Override
	public String getParentKeyStr() {
		return KeyFactory.keyToString(topic);
	}

	@Override
	public String label() {
		return getTitle();
	}
		
	public static DBConversation createConversation(DBTopic topic, String title, String description)
	{
		DBConversation conversation = new DBConversation(title, description, topic);
		
		PMF.pm().makePersistent(conversation);
		
		return conversation;
	}
	
	public static DBConversation getConversation(String keystr)
	{
		Key key = KeyFactory.stringToKey(keystr);
		if(key == null)
			return null;
		
		DBConversation res = (DBConversation)PMF.pm().getObjectById(DBConversation.class, key);
		return res;
	}
	
	public static List<DBConversation> getAllConversationWithTopic(DBTopic top)
	{
		List<DBConversation> res;
		
		Query q = PMF.pm().newQuery(DBConversation.class);	
		q.setFilter("topic == argTopic");
		q.setOrdering("dateLastComment desc");
		q.declareParameters("com.google.appengine.api.datastore.Key argTopic");
		
		res = (List<DBConversation>)q.execute(top.getKey());
		
		return res;
	}
	
	public static List<DBConversation> getConversationsWithTopic(DBTopic top, long first, long last)
	{
		List<DBConversation> res;
		
		Query q = PMF.pm().newQuery(DBConversation.class);	
		q.setFilter("topic == argTopic");
		q.setOrdering("dateLastComment desc");
		q.setRange(first, last);
		q.declareParameters("com.google.appengine.api.datastore.Key argTopic");
		
		res = (List<DBConversation>)q.execute(top.getKey());
		
		return res;
	}
	
	@Override
	public PathableClass getPathable() {
		return pathable;
	}

	@Override
	public List<? extends IPathable> getChilds(int fromNumber, int toNumber) {
		return DBComment.getCommentsOfConversation(this, fromNumber, toNumber);
	}

	@Override
	public List<? extends IPathable> getChilds() {
		return DBComment.getAllCommentsOfConversation(this);
	}
	
	@Override
	public boolean authorizedAction(ActionType actionType) {
		switch(actionType)
		{
		case createChild:
			if(ForumAPI.loggedUser() == true)
				return true; // Logger user can post a response in a conversation
			else
				return false;
		}
		return false;
	}

	@Override
	public boolean applyAction(Action action) {
		// TODO: fillAction is called on the childType, applyAction shall be called on the child too.
		String comment = action.getField("Comment").defaultValue;
		DBUser user = ForumAPI.userLogin();
		
		DBComment dbcomment = DBComment.createComment(this, comment, user);
		if(dbcomment == null)
			return false;
		
		return true;
	}
}
